package huzh.foundation.appserver;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;

import huzh.foundation.log.LogMgr;

/**
 *<p>title:AppServerMgr</p>
 *<p>description:</p>
 * @since 2014-6-8
 * @author huzh
 * @version 1.0
 */
public class AppServerMgr {
    /**bInited*/
    private static boolean bInited = false;
    /**appServerService*/
    private static AbstractAppServerService appServerService = null;

    public static synchronized void init() { 
        if (bInited)
            return;
        try{
            if (isWLS()) {
                appServerService = new WeblogicUtil();
            } else if (isIAS()) {
                appServerService = new IplanetUtil();
            } else if (isSunOne()) {
                appServerService = new SunOneUtil();
            } else if (isJBoss()) {
                appServerService = new JBossUtil();
            } else if (isTomcat()) {
                appServerService = new TomcatUtil();
            }
            AbstractAppServerService.init();
            appServerService.getInitialContext();
            LogMgr.debug(AppServerMgr.class, "AppServer[" + AppServerEnv.getInstance() + "] inited successfully!");
            bInited = true;
        } catch (Exception ex) {
            System.out.println("AppServer inited failed!");
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }

    /**
     * @param dsJNDIName
     * @return
     * @throws NamingException
     */
    public static DataSource getDataSource(String dsJNDIName) throws NamingException {
        if (!bInited) {
            init();
        }
        return appServerService.getDataSource(dsJNDIName);
    }

    /**
     * @return
     * @throws NamingException
     */
    public static UserTransaction getUserTx() throws NamingException {
        if (!bInited) {
            init();
        }
        return appServerService.getUserTx();
    }

    /**
     * @return
     * @throws NamingException
     */
    public static Context getEnvContext() throws NamingException {
        if (!bInited) {
            init();
        }
        return appServerService.getEnvContext();
    }

    /**
     * @return
     * @throws NamingException
     */
    public static InitialContext getInitialContext() throws NamingException {
        if (!bInited) {
            init();
        }
        return appServerService.getInitialContext();
    }

    /**
     * @param jndiName
     * @return
     * @throws NamingException
     */
    public static Object getEjbHomeObjectRef(String jndiName) throws NamingException{
        if (!bInited) {
            init();
        }
        return getInitialContext().lookup(jndiName);
    }

    /**
     * @param jndiName
     * @param ejbHomeClass
     * @return
     * @throws NamingException
     */
    @SuppressWarnings("unchecked")
    public static Object getEjbHomeObject(String jndiName, Class ejbHomeClass) throws NamingException {
        if (!bInited) {
            init();
        }
        return PortableRemoteObject.narrow(getEjbHomeObjectRef(jndiName), ejbHomeClass);
    }

    /**
     * @param ref
     * @param ejbHomeClass
     * @return
     * @throws NamingException
     */
    @SuppressWarnings("unchecked")
    public static Object getEjbHomeObject(Object ref, Class ejbHomeClass) throws NamingException {
        if (!bInited) {
            init();
        }
        return PortableRemoteObject.narrow(ref, ejbHomeClass);
    }

    /**
     * @return
     */
    public static boolean isWLS() {
        return AppServerEnv.getInstance().getAppserverName().equalsIgnoreCase("weblogic");
    }

    /**
     * @return
     */
    public static boolean isIAS(){
        return AppServerEnv.getInstance().getAppserverName().equalsIgnoreCase("iplanet");
    }

    /**
     * @return
     */
    public static boolean isSunOne(){
        return AppServerEnv.getInstance().getAppserverName().equalsIgnoreCase("sunone");
    }

    /**
     * @return
     */
    public static boolean isJBoss(){
        return AppServerEnv.getInstance().getAppserverName().equalsIgnoreCase("jboss");
    }

    /**
     * @return
     */
    public static boolean isTomcat(){
        return AppServerEnv.getInstance().getAppserverName().equalsIgnoreCase("tomcat");
    }
}
